//
//  LSHomeLoadingView.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/9/25.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSHomeLoadingView.h"

@implementation LSHomeLoadingView {
    UIImageView *_loading;
    UILabel *_loadingText;
    UIView *_backView ;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    UIView *backView = [[UIView alloc] init];
    _backView = backView;
    [self addSubview:backView];
    backView.layer.cornerRadius = 7;
    backView.layer.masksToBounds = YES;
    backView.backgroundColor = [UIColor colorWithHex:0xf0f0f1];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.top.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    UIImageView *loading = [[UIImageView alloc] init];
    _loading = loading;
    [self addSubview:loading];
    
    UILabel *loadingText = [[UILabel alloc] init];
    _loadingText = loadingText;
    _loadingText.font = [UIFont systemFontOfSize:13*ScalesW];//FONT(13*ScalesW);
    [self addSubview:loadingText];
}

- (void)setIndex:(NSInteger)index {
    _index = index;
    
    if (_index==0)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"1-0_Loading-RED-Loop" ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        _loading.image = [UIImage sd_animatedGIFWithData:data];
        _loadingText.text = @"小鸽为您听力能力智能分析中...";
        _loadingText.textAlignment = NSTextAlignmentLeft;
        [_loading mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(30);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(100*ScalesW, 90*ScalesW));
        }];
        [_loadingText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_loading.mas_right).offset(10);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(200*ScalesW, 17*ScalesW));
        }];
        
    } else if (_index==1)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"1-0_Loading-Blue-Loop" ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        _loading.image = [UIImage sd_animatedGIFWithData:data];
        _loadingText.text = @"小鸽为您口语能力智能分析中...";
        
        [_loading mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-30);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(100*ScalesW, 90*ScalesW));
        }];
        [_loadingText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_loading.mas_left).offset(-10);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(200*ScalesW, 17*ScalesW));
        }];
        _loadingText.textAlignment = NSTextAlignmentRight;
        
    } else
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"1-0_Loading-GREEN-Loop" ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        _loading.image = [UIImage sd_animatedGIFWithData:data];
        _loadingText.text = @"小鸽为您词汇量智能分析中...";
        _loadingText.textAlignment = NSTextAlignmentLeft;
        [_loading mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(30);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(100*ScalesW, 90*ScalesW));
        }];
        [_loadingText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_loading.mas_right).offset(10);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(200*ScalesW, 17*ScalesW));
        }];
    }
    
}

- (void)setNewindex:(NSInteger)newindex {
    _newindex = newindex;
    
     _loadingText.textAlignment = NSTextAlignmentCenter;
    
    if (_newindex == 0) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"1-0_Loading-RED-Loop" ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        _loading.image = [UIImage sd_animatedGIFWithData:data];
        _loadingText.text = @"小鸽为您听力能力重新分析中...";
       
    } else if (_newindex == 1) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"1-0_Loading-Blue-Loop" ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        _loading.image = [UIImage sd_animatedGIFWithData:data];
        _loadingText.text = @"小鸽为您口语能力重新分析中...";
        
    } else if (_newindex == 2) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"1-0_Loading-GREEN-Loop" ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        _loading.image = [UIImage sd_animatedGIFWithData:data];
        _loadingText.text = @"小鸽为您词汇量重新分析中...";
    }
    
    
    [_loading mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(-90*ScalesW/3);
        make.left.equalTo(self).offset((kWidth-100*ScalesW)/2);
        make.size.mas_equalTo(CGSizeMake(100*ScalesW, 90*ScalesW));
    }];
    
    [_loadingText mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset((kWidth-200*ScalesW)/2);
        make.top.equalTo(_loading.mas_bottom).offset(17*ScalesW);
        make.size.mas_equalTo(CGSizeMake(200*ScalesW, 17*ScalesW));
    }];
}


- (void)stopAnimatingGif {
    [_loading stopAnimating];
    _loading.image = [UIImage new];
    NSLog(@"%@",_loading);
    [[SDImageCache sharedImageCache] clearMemory];
}

@end
